package org.example.bean;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.example.enums.GridType;
import org.example.util.ExceptionUtil;
import org.example.util.StringUtil;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * {@code @Desc:地图管理器}
 *
 * @Author: yanghongjun
 * @Date: 2023/12/20
 **/
public class MapMgr {
    private static final Logger logger = LogManager.getLogger(MapMgr.class);

    /**
     * 用枚举来实现单例
     */
    private enum Singleton {
        INSTANCE;
        final MapMgr processor;

        Singleton() {
            this.processor = new MapMgr();
        }

        MapMgr getProcessor() {
            return processor;
        }
    }

    /**
     * 获取实例对象
     */
    public static MapMgr getInstance() {
        return Singleton.INSTANCE.getProcessor();
    }


    /**
     * 初始化疆域
     *
     * @param gameMap
     */
    public void initMapGridTerritory(GameMap gameMap) {
        try {
            logger.info("initMapGridBlockArray start");
            File file = new File("src/main/resources/Territory.txt");
            List<String> list = Files.readAllLines(file.toPath());
            for (int i = 0; i < list.size(); i++) {
                List<Integer> integers = StringUtil.convertIdsToIntList(list.get(i), " ");
                for (int j = 0; j < integers.size(); j++) {
                    Integer territory = integers.get(j);
                    if (territory > 0) {
                        gameMap.getGrids()[j][i].setTerritory(territory);
                        gameMap.getGrids()[j][i].setOpenTerritory(true);
                    }
                }
            }
            logger.info("initMapGridTerritory end");
        } catch (Exception e) {
            logger.error("MapMgr.initMapGridTerritory error:{}", ExceptionUtil.show(e));
            System.exit(-1);
        }
    }


    /**
     * 初始化阻挡
     *
     * @param gameMap
     */
    public void initMapGridBlockArray(GameMap gameMap) {
        try {
            logger.info("initMapGridBlockArray start");
            File file = new File("src/main/resources/Astar.txt");
            List<String> list = Files.readAllLines(file.toPath());
            for (int i = 0; i < list.size(); i++) {
                String s = StringUtil.takeOutInvisible(list.get(i));
                if (StringUtil.isEmpty(s)) {
                    continue;
                }
                char[] charArray = s.toCharArray();
                for (int j = 0; j < charArray.length; j++) {
                    char c = charArray[j];
                    if (c != '0') {
                        int indexX = i * 10;
                        int indexY = j * 10;
                        for (int x = 0; x < 10; x++) {
                            for (int y = 0; y < 10; y++) {
                                gameMap.addBlock(indexY + y, indexX + x);
                            }
                        }
                        Grid grid = gameMap.getGrids()[j][i];
                        grid.setStopBlock(true);
                        grid.setType(GridType.STATIC_BLOCKING);
                    }
                }
            }
            logger.info("initMapGridBlockArray end");
        } catch (Exception e) {
            logger.error("MapMgr.initMapGridBlockArray error:{}", ExceptionUtil.show(e));
            System.exit(-1);
        }
    }


}
